//
//  SportNet.m
//  SportSocial
//
//  Created by yull on 14/12/13.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "SportNet.h"

@implementation SportNet

+ (AFHTTPRequestOperation *)sportListWithSuccessBlock:(httpSuccessBlock)successBlock
                                            failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/sport/list",[self baseUrl]];
    
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)sportListSuccessBlock:(httpSuccessBlock)successBlock
                                 failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/sport/listHobby",[self baseUrl]];

    NSDictionary *dict = @{@"token":[self token]};
    
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)addToHobbyWithSportName:(NSString *)sportName
                          successBlock:(httpSuccessBlock)successBlock
                             failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/sport/addToHobby",[self baseUrl]];
    
    NSDictionary *dict = @{@"token":[self token],
                           @"sport_name":sportName};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)deleteSportName:(NSString *)sportName
                               successBlock:(httpSuccessBlock)successBlock
                                  failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/sport/deleteFromHobby",[self baseUrl]];
    
    NSDictionary *dict = @{@"token":[self token],
                           @"sport_name":sportName};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)paginatePlanWithPageNumber:(NSInteger)pageNumber
                                              pageSize:(NSInteger)pageSize
                                          successBlock:(httpSuccessBlock)successBlock
                                             failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/sport/paginatePlan",[self baseUrl]];
    
    NSDictionary *dict = @{@"token":[self token],
                           @"page_number":[NSNumber numberWithInt:pageNumber],
                           @"page_size":[NSNumber numberWithInt:pageSize]};
    
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)uploadPlanWithPlanArray:(NSArray *)planArray
                                       successBlock:(httpSuccessBlock)successBlock
                                          failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/sport/uploadPlan",[self baseUrl]];
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:planArray options:kNilOptions error:nil];
    NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSDictionary *dict = @{@"token":[self token],
                           @"data":jsonStr};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)deletePlanWithPlanIdArray:(NSArray *)planIdArray
                                         successBlock:(httpSuccessBlock)successBlock
                                            failBlock:(httpFailBlock)failBlock
{
    NSMutableString *url = [[NSMutableString alloc] initWithFormat:@"%@/sport/deletePlan?",[self baseUrl]];
    [url appendFormat:@"%@",[self token]];
    
    for (NSString *planId in planIdArray) {
        [url appendFormat:@"&plan_ids=%@",planId];
    }
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)paginateHistoryWithPageNumber:(NSInteger)pageNumber
                                                 pageSize:(NSInteger)pageSize
                                             successBlock:(httpSuccessBlock)successBlock
                                                failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/sport/paginateHistory",[self baseUrl]];
    
    NSDictionary *dict = @{@"token":[self token],
                           @"page_number":[NSNumber numberWithInt:pageNumber],
                           @"page_size":[NSNumber numberWithInt:pageSize]};
    
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)uploadHistoryWithHistoryArray:(NSArray *)historyArray
                                             successBlock:(httpSuccessBlock)successBlock
                                                failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/sport/uploadHistory",[self baseUrl]];
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:historyArray options:kNilOptions error:nil];
    NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSDictionary *dict = @{@"token":[self token],
                           @"data":jsonStr};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)deleteHistoryWithHistoryIdArray:(NSArray *)historyIdArray
                                               successBlock:(httpSuccessBlock)successBlock
                                                  failBlock:(httpFailBlock)failBlock
{
    NSMutableString *url = [[NSMutableString alloc] initWithFormat:@"%@/sport/deleteHistory?",[self baseUrl]];
    [url appendFormat:@"%@",[self token]];
    
    for (NSString *planId in historyIdArray) {
        [url appendFormat:@"&history_ids=%@",planId];
    }
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

//sport_name 运动；plan_date 计划时间
+ (AFHTTPRequestOperation *)uploadSportPlanWithSportName:(NSString *)sportName
                                                    date:(NSString *)date
                                            successBlock:(httpSuccessBlock)successBlock
                                               failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/sport/uploadPlan",[self baseUrl]];
    NSDictionary *dataDict = @{@"sport_name":sportName,
                               @"plan_date":date};
    NSArray *dataArray = @[dataDict];
    NSData *data = [NSJSONSerialization dataWithJSONObject:dataArray options:kNilOptions error:nil];
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *param = @{@"token":[self token],
                            @"data":dataStr};
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *message = [self handleResultCode:[[responseObject objectForKey:@"resultCode"] integerValue]];
        if (message.length == 0) {
            successBlock (operation,responseObject);
        }else {
            failBlock (operation,nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,nil);
    }];
    return operation;
}
@end
